package spms.data.util;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Page<T> {

    private List<T> contentList = null;
    private JSONArray contentJA = null;

    private Integer pageNum;//当前页码
    private Integer pageSize;//页面大小
    private Integer totalNum;//总数据量
    private Integer totalPage;//总页数

    public Page(){}

    public Page(Integer pageNum, Integer pageSize, Integer totalNum, List<T> contentList){
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.totalNum = totalNum;
        this.contentList = contentList;
        initialPage();
    }

    private void initialPage(){
        this.totalPage = totalNum%pageSize == 0 ? totalNum/pageSize : totalNum/pageSize+1;

        getContentList().forEach(item ->{
            JSONObject job = JSONObject.parseObject(JSONObject.toJSONString(item));
            getContentJA().add(job);
        });

    }

    public List<T> getContentList() {
        if(contentList == null){
            return new ArrayList<>();
        }
        return contentList;
    }

    public void setContentList(List<T> contentList) {
        this.contentList = contentList;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalNum() {
        return totalNum;
    }

    public void setTotalNum(Integer totalNum) {
        this.totalNum = totalNum;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public JSONArray getContentJA() {
        if(contentJA == null){
            contentJA = new JSONArray();
        }
        return contentJA;
    }

    public void setContentJA(JSONArray contentJA) {
        this.contentJA = contentJA;
    }

	@Override
	public String toString() {
		return "Page [contentList=" + contentList + ", contentJA=" + contentJA + ", pageNum=" + pageNum + ", pageSize="
				+ pageSize + ", totalNum=" + totalNum + ", totalPage=" + totalPage + "]";
	}
    
}
